using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using StupidSpaceThing.MenuSystem;
using Microsoft.Xna.Framework.Graphics;

namespace StupidSpaceThing
{
	internal class Mine : Unit
	{
		public static new int _ProductionCost = 300;
		public override int ProductionTime { get { return _ProductionTime; } }
		public override int ProductionCost { get { return _ProductionCost; } }
		private bool triggered = false;
		public bool Triggered { 
			get { return triggered; }
			set { triggered = value; }
		}
		public int RemoveFromTopMenu = 0;
		private const float triggerDistance = 10.0f;
		public float DamageMultiplier = 1;
		public float RangeMultiplier = 1;

		public Mine(Vector3 position)
			: base(new MineEntity(position)) {			
			massModifier = 0;			
		}

		public override void Update(RealGameTime gameTime) {
			if( ((entity.Dead && entity.ScaleFactor == 1) || (triggered && !entity.Dead && (Entity as MineEntity).Spin < -0.15f) )
				&& ((!Game1.Ended && (Faction != null && !Faction.Defeated)) || !TopMenu.GameRunning) ) {
				if( !TopMenu.GameRunning ) {
					if( RemoveFromTopMenu % 2 == 0 ) {
						for( int i = 0; i < 20; i++ ) {
							Vector3 direction = new Vector3((float)(TopMenu.Rand.NextDouble() * Math.PI * 2 - Math.PI), (float)(TopMenu.Rand.NextDouble() * Math.PI * 2 - Math.PI), (float)(TopMenu.Rand.NextDouble() * Math.PI * 2 - Math.PI));
							Color color = new Color((byte)TopMenu.Rand.Next(0, 255), (byte)TopMenu.Rand.Next(0, 255), (byte)TopMenu.Rand.Next(0, 255), 100);
							Bullets.Fire(Bullets.GetBullet(color, this, direction, Position.Y, gameTime, false));
						}
					}
					RemoveFromTopMenu++;
				} else {
					if( !entity.Dead ) { entity.Die(); }
					Game1.CurMap.Bomb(gameTime, Position2D, 100, -5, this);
					foreach( Faction f in Faction.Factions ) {
						foreach( FactionEntity fe in f.FactionEntities ) {
							if( fe.Entity.Dead || fe.Faction == Faction ) continue;
							ColObject colObject = fe.Entity.ColObject;
							if( colObject != null ) {
								Vector2 diff = fe.Position2D - Position2D;
								float length = diff.Length();
								diff.Normalize();
								float force = MathHelper.Lerp(10000, 0, ((float)Math.Pow(length, 0.8)) / 25);
								if( force > 0 ) {
									fe.Entity.ColObject.Body.ApplyForce(diff * force);
								}
								float dmg = MathHelper.Lerp(700 * DamageMultiplier, 0, ((float)Math.Pow(length, 0.8)) / (14 * RangeMultiplier));
								if( dmg > 0 ) {
									fe.Entity.ApplyDamage(dmg, this);
								}
							}
						}
					}
					for( double i = 0; i < Math.PI * 2; i += 0.02f ) {
						Bullets.Fire(Bullets.GetBullet(Faction.Color, this, new Vector3((float)Math.Sin(i), 0, (float)Math.Cos(i)), Position.Y, gameTime, false));
					}
					SoundSystem.PlayCue("mine", Game1.Listener, AudioEmitter);
				}
			} else if(!triggered) {
				if( entity.Life < 40 ) {
					triggered = true;
				} else {
					foreach( Faction faction in Faction.Factions ) {
						if( faction == Faction ) continue;
						foreach( Unit unit in faction.Units ) {
							if( !(unit is Mine) && !(unit is Pylon) && Vector3.Distance(unit.Position, Position) < triggerDistance ) {
								triggered = true;
							}
						}
					}
				}
				if( triggered ) {
					entity.ScaleFactor = 0.1f;
					entity.Heal(100);
				}
			} else if( triggered ) {
				(Entity as MineEntity).Spin -= 0.002f;
				if( entity.ScaleFactor < 1 ) {
					entity.ScaleFactor += 0.02f;
				}
			}
			base.Update(gameTime);			
		}
	}
}
